import easygui as g
import sys
def DengLu():
    fields = ('用户名：', '密码：')
    msg = '请输入用户名和密码：'
    title = '登录'
    yonghu = g.multpasswordbox(msg, title, fields)

    if yonghu == None or yonghu == ['', '']:
        g.msgbox('请登录或注册!', ok_button='确定 ')
        return 2
    else:
        list1 = []
        ZhangHao = open('zhanghao.txt')
        for each_line in ZhangHao:
            (zhanghao_, huiche_) = each_line.split('\n')
            list1.append(zhanghao_)
        ZhangHao.close()
        list2 = []
        MiMa = open('mima.txt')
        for each_line in MiMa:
            (mima_, huiche_) = each_line.split('\n')
            list2.append(mima_)
        MiMa.close()
        for X in list1:
            if X == str(yonghu[0]) and list2[list1.index(X)] != str(yonghu[1]):
                g.msgbox('密码错误，请重新输入!', ok_button='确定 ')
                return 0
                break
            elif X == str(yonghu[0]) and list2[list1.index(X)] == str(yonghu[1]):
                g.msgbox(str(yonghu[0]) + '登陆成功!', ok_button='确定 ')
                return 2
                break
        if str(yonghu[0]) not in list1:
            g.msgbox('账号不存在，请注册：', ok_button='确定 ')
            return 1
def ZhuCe():
    values = []
    def zhuce():
        msg = '*为必填项'
        title = '注册ing~~~'
        fields = ['*用户名', '*密码']
        return g.multenterbox(msg, title, fields, values)
    YongHuZhuCe = zhuce()
    if YongHuZhuCe == None:
        g.msgbox('还请注册!', ok_button='确定 ')
        return 2
    else:
        while YongHuZhuCe[0] == '' or YongHuZhuCe[1] == '':
            g.msgbox('用户名或密码不能为空！', ok_button='继续填写 ')
            values = [YongHuZhuCe[0], YongHuZhuCe[1], YongHuZhuCe[2]]
            YongHuZhuCe = zhuce()
        list3 = []
        ZhangHao = open('zhanghao.txt')
        for each_line in ZhangHao:
            (zhanghao_, huiche_) = each_line.split('\n')
            list3.append(zhanghao_)
        ZhangHao.close()
        while str(YongHuZhuCe[0]) in list3:
            g.msgbox('该用户名已被占用！', ok_button='重新输入 ')
            YongHuZhuCe = zhuce()
        ZhangHao = open('zhanghao.txt', 'a')
        ZhangHao.write(YongHuZhuCe[0] + '\n')
        ZhangHao.close()
        MiMa = open('mima.txt', 'a')
        MiMa.write(YongHuZhuCe[1] + '\n')
        MiMa.close()
        return 0
def main():
    ZhangHao = open('zhanghao.txt', 'w')
    MiMa = open('mima.txt', 'w')
    ZhangHao.close()
    MiMa.close()
while 1:
    choices = ['已有账号，直接登录', '开始注册']
    choice = 0
    choice = g.indexbox('登录/注册：', '请选择：', choices=choices)
    while choice == 0:
        choice = DengLu()
    while choice == 1:
        choice = ZhuCe()
        if choice == 0:
            g.msgbox('注册成功，即将进入登录页面！', ok_button='确定 ')
            choice = DengLu()
    msg = "要再次尝试吗？"
    title = "请选择"
    if g.ccbox(msg, title, choices=('当然啦', '不喽')):
        pass
    else:
        sys.exit(0)
main()